package com.bluesky.dreamer.interceptor;

import java.lang.reflect.Method;

import org.apache.struts2.interceptor.TokenInterceptor;

import com.opensymphony.xwork2.ActionInvocation;

public class MethodTokenInterceptor extends TokenInterceptor {

	private static final long serialVersionUID = 1L;

	protected String doIntercept(ActionInvocation invocation) throws Exception {
		Object action = invocation.getAction();
		if(action != null) {
			Class<? extends Object> actionClass = action.getClass();
			String methodName = invocation.getProxy().getMethod();
            String altMethodName = "do" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
			
            Method method = null;
	        try {
	            method = actionClass.getMethod(methodName, new Class[0]);
	        } catch (NoSuchMethodException e) {
	            method = actionClass.getMethod(altMethodName, new Class[0]);
	        }
	        
			Token token = method.getAnnotation(Token.class);
			if(token != null) {
				return super.doIntercept(invocation);
			}
		}
		return invocation.invoke();
	}	
}
